package com.mk.cloud.pt.controller.platform;

import act.apidoc.Description;
import act.controller.Controller;
import act.inject.DefaultValue;
import com.mk.cloud.common.domain.PWechatMenus;
import com.mk.cloud.common.enums.GlobalValues;
import com.mk.cloud.common.exception.UserException;
import com.mk.cloud.common.interceptor.ITransacte;
import com.mk.cloud.common.interceptor.cookie.CookieService;
import com.mk.cloud.common.interceptor.cookie.ICLogin;
import org.osgl.util.S;
import com.mk.cloud.common.util.S2;
import org.beetl.sql.core.engine.PageQuery;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.PutAction;
import org.osgl.mvc.annotation.With;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("wechatMenus")
public class PWechatMenusController extends CookieService{

    @Inject
    private PWechatMenus.Mapper pWechatMenusMapper;

    @Description("小程序动态菜单--小程序动态菜单列表")
    @With(ICLogin.class)
    @GetAction("pageList.json")
    public PageQuery<PWechatMenus> pProductList(
            @Description("筛选条件-菜单名称") String wmname,
            @Description("筛选条件-企业名称") String coname,
            @Description("当前第几页") @NotNull Integer currentPage,
            @Description("每页多少条") @DefaultValue("15") Integer pageSize

    ) {
        PageQuery<PWechatMenus> query = new PageQuery<>();
        query.setPageSize(pageSize);
        query.setPageNumber(currentPage);
        Map<String, Object> map = new HashMap<>();
        map.put("wmname", wmname);
        map.put("coname", coname);
        map.put("coid", me.getCoid());
        query.setParas(map);
        //此处的sql是三个sql
        pWechatMenusMapper.getSQLManager().pageQuery("pWechatMenus.select_wechat_menus_page_list", PWechatMenus.class, query);
        return query;
    }


    @Description("小程序动态菜单--小程序动态菜单详情")
    @With(ICLogin.class)
    @GetAction("get.json")
    public PWechatMenus getPWechatMenus(@Description("小程序动态菜单id") String sid) {
        if (S.isEmpty(sid)) {
            return new PWechatMenus();
        }
        return pWechatMenusMapper.single(sid);
    }

    @Description("小程序动态菜单--小程序动态菜单保存")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("save.json")
    public String savePWechatMenus(@Description("小程序动态菜单对象") PWechatMenus wechatMenus) {
        wechatMenus.setCoid(me.getCoid());
        if (S.isNotBlank(wechatMenus.getWmid())) {
            pWechatMenusMapper.updateTemplateById(wechatMenus);
            return wechatMenus.getWmid();
        } else {
            String wmid = S2.UUID();
            wechatMenus.setWmid(wmid);
            Integer orderby = pWechatMenusMapper.selectMaxOrderby(me.getCoid());
            if(orderby==null){
                orderby = 1;
            }else {
                orderby += 1;
            }
            wechatMenus.setOrderby(orderby);
            pWechatMenusMapper.insert(wechatMenus);
            return wmid;
        }
    }


    @Description("菜单排序上移")
    @PutAction("upWechatMenus.json")
    @With({ICLogin.class,ITransacte.class})
    public String upWechatMenus(@Description("小程序动态菜单id") String wmid){
        PWechatMenus wechatMenus = pWechatMenusMapper.single(wmid);
        PWechatMenus preMenus = pWechatMenusMapper.selectPreMenu(wechatMenus.getCoid(),wechatMenus.getOrderby());
        UserException.throwIf(preMenus==null,"已经是第一个菜单了");
        wechatMenus.setOrderby(preMenus.getOrderby());
        pWechatMenusMapper.updateTemplateById(wechatMenus);
        preMenus.setOrderby(preMenus.getOrderby()+1);
        pWechatMenusMapper.updateTemplateById(preMenus);
        return wmid;
    }

    @Description("菜单排序下移")
    @PutAction("downWechatMenus.json")
    @With({ICLogin.class,ITransacte.class})
    public String downWechatMenus(@Description("小程序动态菜单id") String wmid){
        PWechatMenus wechatMenus = pWechatMenusMapper.single(wmid);
        PWechatMenus nextMenu = pWechatMenusMapper.selectNextMenu(wechatMenus.getCoid(),wechatMenus.getOrderby());
        UserException.throwIf(nextMenu==null,"已经是最后一个菜单了");
        wechatMenus.setOrderby(nextMenu.getOrderby());
        pWechatMenusMapper.updateTemplateById(wechatMenus);
        nextMenu.setOrderby(nextMenu.getOrderby()-1);
        pWechatMenusMapper.updateTemplateById(nextMenu);
        return wmid;
    }

    @Description("小程序动态菜单--将菜单同步给新的公司")
    @With({ICLogin.class, ITransacte.class})
    @PostAction("saveToCompany.json")
    public String savePWechatMenusToCompany(@Description("公司id") String coid) {
        pWechatMenusMapper.updateOrInsertMenus(coid);
        List<PWechatMenus> list = pWechatMenusMapper.selectAllNewMenus(coid);
        if(list!=null){
            Integer orderby = pWechatMenusMapper.selectMaxOrderby(coid);
            if(orderby==null){
                orderby = 0;
            }
            Map<String,Integer> map = new HashMap<>();
            map.put("orderby",orderby);
            list.forEach(pWechatMenus -> {
                pWechatMenus.setPwmid(pWechatMenus.getWmid());
                pWechatMenus.setCoid(coid);
                map.put("orderby",map.get("orderby")+1);
                pWechatMenus.setOrderby(map.get("orderby"));
                pWechatMenus.setWmid(S2.UUID());
                pWechatMenus.setIsshow(GlobalValues.FLAG_NO.value());
                pWechatMenusMapper.insertTemplate(pWechatMenus);
            });
        }
        return coid;
    }
}
